package com.dsg.controller.coupon;


import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dsg.aspect.NoRepeatSubmit;
import com.dsg.common.constants.MessageConstants;
import com.dsg.common.dto.AjaxResult;
import com.dsg.common.util.SingletonLoginUtils;
import com.dsg.common.util.UUIDGenerator;
import com.dsg.common.util.toolbox.StringUtil;
import com.dsg.controller.base.BaseController;
import com.dsg.entity.coupon.DsgCouponReturn;
import com.dsg.service.coupon.IDsgCouponReturnService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 回赠油券资料 前端控制器
 * </p>
 *
 * @author system
 * @since 2020-07-25
 */
@RestController
@RequestMapping("/coupon/dsgCouponReturn")
@Api(description = "回赠油券资料")
public class DsgCouponReturnController extends BaseController {
    @Autowired
    private IDsgCouponReturnService service;

    @NoRepeatSubmit
    @PostMapping(value = "/add")
    @ApiOperation("新增")
    public AjaxResult add(@RequestBody DsgCouponReturn couponReturn) {
        couponReturn.setOid(UUIDGenerator.getUUID());
        boolean falg = service.save(couponReturn);
        if (falg) {
            return result(MessageConstants.SSO_STATUS_SUCCESS);
        } else {
            return result(MessageConstants.SSO_STATUS_FAIL);
        }
    }

    @NoRepeatSubmit
    @PutMapping(value = "/update")
    @ApiOperation("更新")
    public AjaxResult update(@RequestBody DsgCouponReturn couponReturn) {
        boolean flag = service.updateById(couponReturn);
        if (flag) {
            return json(MessageConstants.SSO_STATUS_SUCCESS, couponReturn);
        } else {
            return result(MessageConstants.SSO_STATUS_FAIL);
        }
    }

    @DeleteMapping(value = "/delete")
    @ApiOperation("删除")
    public AjaxResult delete(String id) {
        Boolean flag = service.removeById(id);
        if (flag) {
            return result(MessageConstants.SSO_STATUS_SUCCESS);
        } else {
            return result(MessageConstants.SSO_STATUS_FAIL);
        }
    }


    /**
     * 查询列表（分页）
     *
     * @param dsgCouponReturn
     * @return
     */
    @GetMapping(value = "/list")
    @ApiOperation("查询列表（分页）")
    public AjaxResult list(DsgCouponReturn dsgCouponReturn) {
        // 分页
        Page page = getPage();
        String sort = getParameter("sort");
        List<OrderItem> orderItemList = new ArrayList<>();
        OrderItem orderItem = new OrderItem();
        if (StringUtil.isEmpty(sort)) {
            orderItem.setColumn("CREATE_TIME");
            orderItem.setAsc(false);
            orderItemList.add(orderItem);
            page.setOrders(orderItemList);
        }
        dsgCouponReturn.setEntCode(SingletonLoginUtils.getSystemEntCode());
        page = service.selectByList(page, dsgCouponReturn);
        // 格式要返回的数据
        AjaxResult ajaxResult = json(MessageConstants.SSO_STATUS_SUCCESS, page.getRecords(), (int) page.getTotal());
        return ajaxResult;
    }
}
